﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoverStudio.Common.PositionSource;
using System.Threading;
using System.Drawing;

namespace WebcamHttp
{
    public class HttpWebcamVideoSource : IVideoSourceCommon
    {
        public HttpWebcamVideoSource()
        {
            PictureUrl = "http://192.168.1.102:8080/cam_1.jpg";
        }

        public string PictureUrl
        {
            get;
            set;
        }

        //private Thread downloadThread = null;
        //private bool threadStop = false;

        Timer timer;
        System.Windows.Forms.Timer wfTimer;

        public void OpenVideoSource()
        {
            //if (downloadThread == null)
            //{
            //    downloadThread = new Thread(DownloadThreadStart);
            //    downloadThread.IsBackground = true;
            //    threadStop = false;
            //    downloadThread.Start();
            //}

            //if (timer == null)
            //    timer = new Timer(TimerTick, this, 0, 1000 / desiredFrameRate);
            //else
            //    timer.Change(0, 1000 / desiredFrameRate);

            if (wfTimer == null)
            {
                wfTimer = new System.Windows.Forms.Timer();
                wfTimer.Tick += new EventHandler(wfTimer_Tick);
            }
            wfTimer.Interval = 1000 / desiredFrameRate;
            wfTimer.Enabled = true;

        }

        void wfTimer_Tick(object sender, EventArgs e)
        {
            lock (this)
            {
                var frame = CameraFrame.GetNextFrame(PictureUrl);
                if (frame != null && NewFrame != null)
                    NewFrame(frame.LastImage as Bitmap);
            }
        }

        void TimerTick(object state)
        {
            lock (this)
            {
                var frame = CameraFrame.GetNextFrame(PictureUrl);
                if (frame != null && NewFrame != null)
                    NewFrame(frame.LastImage as Bitmap);
            }
        }

        public void CloseVideoSource()
        {
            if (wfTimer != null)
                wfTimer.Enabled = false;
//            timer.Change(Timeout.Infinite, Timeout.Infinite);
//            threadStop = true;
        }

        //private void DownloadThreadStart()
        //{
        //    while (!threadStop)
        //    {
        //        var frame = CameraFrame.GetNextFrame(PictureUrl);
        //        if (NewFrame != null)
        //            NewFrame(frame.LastImage as Bitmap);
        //        Thread.Sleep(1000 / desiredFrameRate);
        //    }
        //}


        private int desiredFrameRate = 10;

        public int DesiredFrameRate
        {
            get { return desiredFrameRate; }
            set { desiredFrameRate = value; }
        }



        #region IVideoSourceCommon Members

        public string Name
        {
            get { return "HttpVideoSource"; }
        }

        public bool IsActive
        {
            get { throw new NotImplementedException(); }
        }

        public event NewFrameDelegate NewFrame;

        #endregion
    }
}
